package instantHelper.NormalMode;

import instantHelper.Models.CustomHttpClient;
import instantHelper.Raceway.InstantHelperConstant;
import instantHelper.Raceway.SettingsUtil;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/* Class My Location Listener */
// For sending GPS

public class MyLocationListener implements LocationListener {
	public static String requestID;
	private String androidId;
	private static String className;

	@Override
	public void onLocationChanged(Location loc) {
		//requestID=NormalModeAudioRecorder.getRequestId();
		double latitude = loc.getLatitude();
		double longtitude = loc.getLongitude();
		sendGPSToCentralAPP(latitude, longtitude);
	}

	public static void getGPSData(Context cntx, String androidId,
			String requestId) {
		LocationManager mlocManager = (LocationManager) cntx
				.getSystemService(Context.LOCATION_SERVICE);
		MyLocationListener mlocListener = new MyLocationListener();
		mlocListener.setRequestID(requestId);
		mlocListener.setAndroidId(androidId);
		mlocManager.requestLocationUpdates(getProvider(mlocManager), 100, 1,
				mlocListener);
		mlocListener.onLocationChanged(mlocManager
				.getLastKnownLocation(getProvider(mlocManager)));
	}

	public static String getProvider(LocationManager locManager) {
		String provider = "";
		List<String> providers = locManager.getProviders(true);
		for (int i = 0; i < providers.size(); i++) {
			if (providers.get(i) != null) {
				Location l = locManager.getLastKnownLocation((String) providers
						.get(i));
				if (l != null) {
					provider = providers.get(i).toString();
					break;
				}
			}
		}
		return provider;
	}

	
	

	private String sendGPSToCentralAPP(double latitude, double longtitude) {

		String url = InstantHelperConstant.CentralAppURL
				+ "/androidhelprequest/newGPSData";

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("gpsx", Double
				.toString(latitude)));
		postParameters.add(new BasicNameValuePair("gpsy", Double
				.toString(longtitude)));
		postParameters.add(new BasicNameValuePair("requestId", requestID));
		postParameters.add(new BasicNameValuePair("androidId", androidId));

		String response = null;
		String result = "";
		try {
			response = CustomHttpClient.executeHttpPost(url, postParameters);
			String res = response.toString();
			JSONObject jObject = new JSONObject(res);
			result = jObject.getString("result");

		} catch (Exception e) {

		}
		return result;

	}

	@Override
	public void onProviderDisabled(String provider) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		
	}

	public static void setRequestID(String requestID) {
		MyLocationListener.requestID=requestID;
	}
	

	public String getRequestID() {
		return requestID;
	}

	public void setAndroidId(String androidId) {
		this.androidId = androidId;
	}

	public String getAndroidId() {
		return androidId;
	}
}/* End of Class MyLocationListener */
